const express = require("express")
const {
    isEntryParame,

} = require("../../utils/validate/validate")
const {
    createTime
} = require('../../utils/time/time')
const Router = express.Router()
const configSql = require("../../db/db");
// 增删改查  攻略


// 增
Router.post('/get_strategy', (req, res) => {
    let {
        uid,
        context,
        title,
        address,
        isFlag
    } = req.body
    let obj = {
        uid,
        context,
        title,
        address,
    }
    isEntryParame(obj, (d) => {
        if (!d.boo) {
            res.status(500).send({
                code: 500,
                msg: d.msg,
            });
            return;
        }
    })
    let sql = `insert into strategy_list(uid,context,title,createTime,isFlag,address)values('${uid}','${context}','${title}','${createTime()}','${isFlag}','${address}')`
    configSql(sql).then(result => {
        res.send({
            code: 0,
            msg: '新增成功',
        })
        return
    })
})


// 删
Router.post('/del_strategy', (req, res) => {
    let {
        id
    } = req.body
    let obj = {
        id
    }
    isEntryParame(obj, (d) => {
        if (!d.boo) {
            res.status(500).send({
                code: 500,
                msg: d.msg,
            });
            return;
        }
    })
    let sql = `delete from strategy_list where id='${id}'`
    configSql(sql).then(result => {
        res.send({
            code: 0,
            msg: '删除成功',
        })

    })
})

// 改
Router.post('/update_strategy', (req, res) => {
    let {
        id,
        context,
        title,
        address,
    } = req.body
    let obj = {
        id,
        context,
        title,
        address,
    }
    isEntryParame(obj, (d) => {
        if (!d.boo) {
            res.status(500).send({
                code: 500,
                msg: d.msg,
            });
            return;
        }
    })
    let sql = `update strategy_list set context='${context}',title='${title}','${address}' where id='${id}'`
    configSql(sql).then(result => {
        res.send({
            code: 0,
            msg: '修改成功',
        })

    })
})
// 查(获取全部的(后台管理系统))
Router.post('/select_strategy', (req, res) => {
    let {
        pageSize,
        page
    } = req.body
    pageSize = pageSize ? pageSize : 10
    page = page ? page - 1 : 0

    let sql = `select * from strategy_list limit ${page * pageSize},${pageSize}`
    let s = `select count(*) from strategy_list`

    configSql(s).then(count => {
        configSql(sql).then(result => {
            res.send({
                code: 0,
                msg: '获取成功',
                data: result,
                count: count[0]['count(*)'],
            })
        })
    })


})

// 根据id查询某个
Router.post("/query_article", (req, res) => {
    let {
        id
    } = req.body
    if (!id) {
        res.send({
            code: 500,
            msg: '参数缺失'
        })
        return
    }
    let sql = `select * from strategy_list where id='${id}'`
    configSql(sql).then(result => {
        res.send({
            code: 0,
            msg: '获取成功',
            data: result
        })
    })



})

// 获取最佳攻略
Router.post('/optimum_strategy', (req, res) => {
    let {
        count,
        address,
    } = req.body
    count = count || 5
    let sql = `select * from strategy_list where address='${address}' limit 0 ,${count}`
    configSql(sql).then(result => {
        res.send({
            code: 0,
            msg: '获取成功',
            data: result
        })
    })
})


// 获取最新旅游攻略
Router.post('/news_strategy', (req, res) => {
    let {
        count,
        address
    } = req.body
    count = count || 5
    let sql = `select * from strategy_list where address='${address}' limit 0 ,${count}`
    configSql(sql).then(result => {
        res.send({
            code: 0,
            msg: '获取成功',
            data: result
        })
    })
})

// 获取攻略列表
Router.get("/get_strategy_list", (req, res) => {
    let {
        address
    } = req.query
    let sql = `select * from strategy_list where address='${address}'and isFlag=1`
    configSql(sql).then(result => {
        res.send({
            code: 0,
            msg: '获取成功',
            data: result
        })
    })
})





// 导出路由
module.exports = Router;